#!/bin/bash

PROG=$0

kmoddir=extra

while getopts "n:v:f:k:" opt; do
	case $opt in
		n) pkgname=$OPTARG ;;
		v) pkgver=$OPTARG  ;;
		f) filename=$OPTARG ;;
		k) kmoddir=$OPTARG ;;
	esac
done

if [ -z "${pkgname}" -o -z "${pkgver}" -o -z "${filename}" ]; then
	echo "Usage: $PROG -n <pkgname> -v <pkgver> [-k <kmoddir> ] -f <filename>"
	exit 1
fi

cat >${filename} <<EOF
PACKAGE_NAME="${pkgname}"
PACKAGE_VERSION="${pkgver}"
PACKAGE_CONFIG="/etc/sysconfig/dkms-lustre"
PACKAGE_CONFIG_FALLBACK="/etc/sysconfig/lustre"
# re-evaluate/create dkms.conf on-target based on local build-time products/dependencies
# (like krb5-devel presence) and also upon configure results/errors before dkms builds
PRE_BUILD="lustre-dkms_pre-build.sh \$module \$module_version \$kernelver \$kernel_source_dir \
	 \$arch \$source_tree \$dkms_tree \$kmoddir"
# to ensure all just built Lustre kernel modules will be saved for further install
POST_BUILD="lustre-dkms_post-build.sh \$module \$module_version \$kernelver \$kernel_source_dir \
	 \$arch \$source_tree \$dkms_tree \$kmoddir"
POST_REMOVE="lustre-dkms_post-remove.sh \$module \$module_version \$kernelver \$kernel_source_dir \
	 \$arch \$source_tree \$dkms_tree \$kmoddir"
EOF
if [ $pkgname = "lustre-zfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
BUILD_DEPENDS[0]="zfs"
EOF
fi
cat >>${filename} <<EOF
OPTS=""
flavor=\$(echo \$kernelver | tr '-' '\\n' | tail -1)
if grep -F -q '.' <<< \$flavor ; then
   flavor=default;
fi
if [ -f /usr/src/kfabric/\${flavor}/Module.symvers ]; then
    OPTS="\${OPTS} --with-kfi=/usr/src/kfabric/\${flavor}"
elif [ -f /usr/src/kfabric/default/Module.symvers ]; then
    OPTS="\${OPTS} --with-kfi=/usr/src/kfabric/default"
fi
if [ -d /usr/src/ofa_kernel/\${flavor} ]; then
    OPTS="\${OPTS} --with-o2ib=/usr/src/ofa_kernel/\${flavor}"
elif [ -d /usr/src/ofa_kernel/default ]; then
    OPTS="\${OPTS} --with-o2ib=/usr/src/ofa_kernel/default"
fi
# Default is to not enable weak-modules
no_weak_modules=yes
strip_default=no
[[ -r \${PACKAGE_CONFIG} ]] || PACKAGE_CONFIG=\${PACKAGE_CONFIG_FALLBACK}
if [[ -r \${PACKAGE_CONFIG} ]] ; then
   source \${PACKAGE_CONFIG}
   shopt -q -s extglob
   if [[ -n \${LUSTRE_DKMS_DISABLE_STRIP} ]] ; then
	[[ \${LUSTRE_DKMS_DISABLE_STRIP,,} == @(y|yes) ]] && strip_default=no
   fi
   if [[ -n \${LUSTRE_DKMS_ENABLE_STRIP} ]] ; then
	[[ \${LUSTRE_DKMS_ENABLE_STRIP,,} == @(y|yes) ]] && strip_default=yes
   fi
   if [[ -n \${LUSTRE_DKMS_ENABLE_GSS} ]] ; then
      [[ \${LUSTRE_DKMS_ENABLE_GSS,,} == @(y|yes) ]] &&
         OPTS="\${OPTS} --enable-gss" ||
         OPTS="\${OPTS} --disable-gss"
   fi
   [[ -n \${LUSTRE_DKMS_CONFIGURE_EXTRA} ]] &&
      OPTS="\${OPTS} \${LUSTRE_DKMS_CONFIGURE_EXTRA}"
   if [[ -n \${LUSTRE_DKMS_WEAK_MODULES} ]] ; then
      [[ \${LUSTRE_DKMS_WEAK_MODULES,,} == @(y|yes) ]] &&
         no_weak_modules="no"
   fi
fi
echo "\${OPTS} " | grep -E -q -- '--disable-gss[^-]|--enable-gss[^-]'
if [[ \$? != 0 ]] ; then
    # User did not force, guess for rpm distros
    rpm -qa | grep krb5-devel >/dev/null
    [[ \$? == 0 ]] &&
         OPTS="\${OPTS} --enable-gss" ||
         OPTS="\${OPTS} --disable-gss"
fi
NO_WEAK_MODULES="\$no_weak_modules"
AUTOINSTALL="yes"
MAKE[0]="make -j \`nproc\`"
# Determine which, if any, o2ib to install:
can_o2ib_mofed=no
can_o2ib_in_kernel=no
ext_mofed=no
int_mofed=no
ksrc=\$(dirname \${kernel_source_dir})/source
[[ -d \$ksrc/include/linux ]] || ksrc=\${kernel_source_dir}
[[ -f \${ksrc}/include/rdma/rdma_cm.h ]] &&
[[ -f \${ksrc}/include/rdma/ib_cm.h ]] &&
[[ -f \${ksrc}/include/rdma/ib_verbs.h ]] &&
   can_o2ib_in_kernel=yes
# Now check if ext_o2ib is explicit, or will be auto-detected:
o2ib=\$(echo \${OPTS} | tr ' ' '\\n' | grep -- '--with-o2ib=' | cut -c 13-)
if [[ "x\$o2ib" = "xno" ]] ; then
   can_o2ib_in_kernel=no
elif [[ -d \$o2ib ]] ; then
   # User specified path, if the build completes it will be ext_ofed
   can_o2ib_mofed=yes
   ext_mofed=yes
else
   # could be either, ext_ofed will be preferred by default
   if command -v ofed_info >/dev/null ; then
      pkgs="mlnx-ofed-kernel-dkms|mlnx-ofed-kernel-modules|mlnx-ofa_kernel-devel"
      pkgs="\${pkgs}|compat-rdma-devel|kernel-ib-devel|ofa_kernel-devel"
      paths=\$(rpm -qa |
              grep -E -w "\${pkgs}" | xargs rpm -ql 2>/dev/null |
              grep -E '/ofa_kernel|/ofa_kernel/default|/openib' |
              grep -v /ofed_scripts/ | head -n1)
      if [[ -n "\$paths" ]]; then
          epaths=\$(find \$paths -name rdma_cm.h |
                   grep -F -e "\$(uname -r)" -e default |
                   sed -e 's:/include/rdma/rdma_cm.h::')
          if [[ \$(echo \$epaths | wc -w) -eq 1 ]]; then
             can_o2ib_mofed=yes
             ext_mofed=yes
          fi
      fi
   fi
fi
[[ "x\${ext_mofed}" = "xno" ]] && [[ x\${can_o2ib_in_kernel} = "xyes" ]] &&
   int_mofed=yes

module() {
  local built_name=\$1
  local built_loc=\$2
  local dest=\$3
  local do_strip=\$4

  [[ -z \$do_strip ]] && do_strip=\${strip_default}
  BUILT_MODULE_NAME[\${#BUILT_MODULE_NAME[@]}]="\${built_name}"
  BUILT_MODULE_LOCATION[\${#BUILT_MODULE_LOCATION[@]}]="\${built_loc}/"
  DEST_MODULE_LOCATION[\${#DEST_MODULE_LOCATION[@]}]="/${kmoddir}/\${dest}/"
  STRIP[\${#STRIP[@]}]=\${do_strip}
}
# Common modules for both Client & Server
module libcfs        libcfs/libcfs      lnet
module lnet_selftest lnet/selftest      lnet
module lnet          lnet/lnet          lnet
module ksocklnd      lnet/klnds/socklnd lnet
flavor=\$(echo \$kernelver | tr '-' '\n' | tail -1)
if [ -f /usr/src/kfabric/\${flavor}/Module.symvers -o -f /usr/src/kfabric/default/Module.symvers ] ; then
    module kkfilnd   lnet/klnds/kfilnd  lnet
fi
# Guessing if gni packages are installed
pkg-config --cflags cray-gni cray-gni-headers cray-krca lsb-cray-hss > /dev/null 2>&1
if [ \$? -eq 0 ] ; then
    module kgnilnd   lnet/klnds/gnilnd  lnet
fi
# in-kernel-o2ib or mofed
if [ "x\${ext_mofed}" = "xyes" ] ; then
    module ko2iblnd lnet/klnds/o2iblnd lnet
elif [ "x\${int_mofed}" = "xyes" ] ; then
    module ko2iblnd lnet/klnds/in-kernel-o2iblnd lnet
fi
# Lustre client modules:
# MODULE NAME    PATH TO MODULE               INSTALL PATH
# -------------  -------------------------    ------------
module ec        lustre/ec                    lustre
module fid       lustre/fid                   lustre
module fld       lustre/fld                   lustre
module lmv       lustre/lmv                   lustre
module lov       lustre/lov                   lustre
module lustre    lustre/llite                 lustre
module mdc       lustre/mdc                   lustre
module mgc       lustre/mgc                   lustre
module obdclass  lustre/obdclass              lustre
module obdecho   lustre/obdecho               lustre
module osc       lustre/osc                   lustre
module ptlrpc    lustre/ptlrpc                lustre
if grep -E -q -- '--enable-gss[^-]' <<< \${OPTS}; then
    module ptlrpc_gss lustre/ptlrpc/gss fs/lustre
fi
# Lustre client kunit modules, --enable-tests
if grep -q ' --enable-tests' <<< \${OPTS}; then
    # MODULE NAME              PATH TO MODULE   INSTALL PATH
    # ----------------------   --------------   ------------
    module obd_mod_rpcs_test   lustre/kunit     lustre
    module kinode              lustre/kunit     lustre
    module obd_test            lustre/kunit     lustre
fi
EOF
if [ $pkgname != "lustre-client" ] ; then
	cat >>${filename} <<EOF
# Lustre server Modules
# MODULE NAME     PATH TO MODULE    INSTALL PATH
# -------------   ---------------   ------------
module ofd        lustre/ofd        lustre
module lfsck      lustre/lfsck      lustre
module mgs        lustre/mgs        lustre
module lquota     lustre/quota      lustre
module mdt        lustre/mdt        lustre
module osp        lustre/osp        lustre
module mdd        lustre/mdd        lustre
module lod        lustre/lod        lustre
module osd_wbcfs  lustre/osd-wbcfs  lustre
EOF
fi
if [ $pkgname = "lustre-zfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
# Module for ZFS backing
module osd_zfs   lustre/osd-zfs  lustre
EOF
fi
if [ $pkgname = "lustre-ldiskfs" -o $pkgname = "lustre-all" ] ; then
	cat >>${filename} <<EOF
# Module for ldiskfs backing
module osd_ldiskfs   lustre/osd-ldiskfs  lustre
# ldiskfs module
module ldiskfs       ldiskfs             lustre
# Lustre server kunit modules, --enable-tests
if grep -q ' --enable-tests' <<< \${OPTS}; then
    # MODULE NAME          PATH TO MODULE   INSTALL PATH
    # ------------------   --------------   ------------
    module llog_test       lustre/kunit     lustre
    module ldlm_extent     lustre/kunit     lustre
fi
EOF
fi
